package com.tanxi521.springboot.utils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;

public class DownLoadUtils {

    public static void download(String urlString, String playerName, String savePath) throws Exception {
        URL url = new URL(urlString);
        URLConnection con = url.openConnection();
        con.setConnectTimeout(50 * 1000);
        InputStream is = con.getInputStream();
        byte[] bs = new byte[8192];
        int len;
        File sf = new File(savePath);
        if (!sf.exists()) {
            sf.mkdirs();
        }
        String newFileName = playerName + ".png";
        OutputStream os = new FileOutputStream(sf.getPath() + "\\" + newFileName);
        while ((len = is.read(bs)) != -1) {
            os.write(bs, 0, len);
        }
        os.close();
        is.close();
    }
}
